﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace VK.Comm.TransactSql
{
    public static class DbHelper
    {
        public static void PackDbCommandByDbParameters(DbCommand command, DbParameter[] parameterArray)
        {
            foreach (DbParameter parameter in parameterArray)
            {
                command.Parameters.Add(parameter);
            }
        }

        public static DbParameter PackInParameter(Database db, string parameterName, DbType dbType, object value)
        {
            DbParameter parameter = PackParameter(db, parameterName, dbType, value, ParameterDirection.Input);
            return parameter;
        }

        public static DbParameter PackOutParameter(Database db, string parameterName, DbType dbType, object value)
        {
            DbParameter parameter = PackParameter(db, parameterName, dbType, value, ParameterDirection.Output);
            return parameter;
        }

        private static DbParameter PackParameter(Database db, string parameterName, DbType dbType, object value, ParameterDirection direction)
        {
            DbParameter parameter = db.DbProviderFactory.CreateParameter();
            parameter.DbType = dbType;
            parameter.Direction = direction;
            parameter.ParameterName = parameterName;
            parameter.Value = value;
            return parameter;
        }
    }
}
